package cn.kgc.controller;

import cn.kgc.pojo.DataAnalysis;
import cn.kgc.service.DataAnalysisService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 木子
 * @date: 2022/2/23
 * @description:
 */
@Controller
@RequestMapping("/data")
public class DataAnalysisController {

    @Resource
    private DataAnalysisService ds;

    @RequestMapping("/kan")
    public String kan(){
        return "lxmine/dataAnalysis";
    }

    //显示数据
    @RequestMapping("/show")
    @ResponseBody
    public String show(@RequestParam String dataA,
                       @RequestParam Integer pageSize,
                       @RequestParam Integer currentPage){
        System.out.println("pageSize = " + pageSize);
        System.out.println("currentPage = " + currentPage);
        PageHelper.startPage(currentPage, pageSize);
        Page<DataAnalysis> total = ds.total(dataA);
        List<DataAnalysis> timeAll = ds.timeAll();
        PageInfo<DataAnalysis>  pag = new PageInfo<DataAnalysis> (total);
        Map<String,Object> map = new HashMap<>();
        map.put("data",pag);
        map.put("time",timeAll);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/goDetails")
    public String goDetails(){
        return "lxmine/dataDetails";
    }

    @RequestMapping("/details/{data}")
    public String details1(@PathVariable String data, Model model){
        List<DataAnalysis> list = ds.details1(data);
        model.addAttribute("data",list);
        model.addAttribute("nian",data);
        return "lxmine/dataDetails";
    }
    @RequestMapping("/details2/{data}")
    @ResponseBody
    public String details2(@PathVariable String data){
        List<DataAnalysis> list = ds.details1(data);
        return JSON.toJSONString(list);
    }
}
